<?php

namespace Home\Controller;
use Think\Controller;
use Home\Common\CommonController;
/**
 * 个人中心功能块
 */
class SettingController extends CommonController {
	public function people() {

		// 资料更新
		if (IS_POST) {

			$birthday = $_POST['birthday_y'] . '-' . $_POST['birthday_m'] . '-' . $_POST['birthday_d'];
			$model = M('user');
			//实现空资料不插入
			empty($_POST['sex']) ? : $sql['sex'] = $_POST['sex'];
			empty($_POST['birthday_y']) || empty($_POST['birthday_m']) || empty($_POST['birthday_d']) ? : $sql['birthday'] = $birthday;
			empty($_POST['province']) ? : $sql['province'] = $_POST['province'];
			empty($_POST['city']) ? : $sql['city'] = $_POST['city'];
			empty($_POST['jobs']) ? : $sql['jobs'] = $_POST['jobs'];
			empty($_POST['signature']) ? : $sql['introduce'] = $_POST['signature'];
			empty($_POST['qq']) ? : $sql['qq'] = $_POST['qq'];
			empty($_POST['mobile']) ? : $sql['phone'] = $_POST['mobile'];
			empty($_POST['common_email']) ? : $sql['email'] = $_POST['common_email'];
			empty($_POST['homepage']) ? : $sql['site'] = $_POST['homepage'];

			if ($sql == array()) {

				$this -> error('您未填写任意一项有效资料');
			}

			$execute = $model -> where('id = ' . $_SESSION['id']) -> save($sql);
			// $sql = "UPDATE `user` SET `sex` = '$_POST[sex]',SET `birthday` = '$birthday',SET `province` = '$_POST[province]',SET `city` = '$_POST[city]',SET `jobs` = '$_POST[jobs]',SET `introduce` = '$_POST[signature]',SET `qq` = '$_POST[qq]',SET `phone` = '$_POST[mobile]',SET `email` = '$_POST[common_email]',SET `site` = '$_POST[homepage]' WHERE `id` = '$_SESSION[id]'";
			// $execute = $model->execute($sql);
		} else {

			$this -> display();
		}

	}

	public function edu_ajax() {
		//页面加载读取教育经验
		if (IS_POST) {

			$sql = "SELECT * FROM `education` a WHERE a.uid = '$_POST[id]'";

			$model = M();

			$result = $model -> query($sql);

			echo json_encode($result);
		}

	}

	public function work_ajax() {
		//页面加载读取工作经历
		$sql = "SELECT * FROM `work` a WHERE a.uid = '$_POST[id]'";

		$model = M();

		$result = $model -> query($sql);

		echo json_encode($result);
	}

	public function add_edu() {
		// 添加教育经历
		if (IS_POST) {

			foreach ($_POST as $value) {
				if ($value == '') {
					$data = 0;
					echo json_encode($data);
					return;
				}
			}
			$user = M('education');

			$sql['school'] = $_POST['school_name'];
			$sql['profession'] = $_POST['departments'];
			$sql['year'] = $_POST['education_years'];
			$sql['uid'] = $_SESSION['id'];

			$result = $user -> add($sql);

			$data['id'] = $result;
			$data['errno'] = 1;

			echo json_encode($data);

		}

	}

	public function add_work() {
		// 添加工作经历
		if (IS_POST) {

			foreach ($_POST as $value) {

				if ($value == '') {
					$data = 0;
					echo json_encode($data);
					return;
				}
			}
			$user = M('work');

			$sql['company'] = $_POST['company_name'];
			$sql['job'] = $_POST['job_id'];
			$sql['start'] = $_POST['start_year'];
			$sql['end'] = $_POST['end_year'];
			$sql['uid'] = $_SESSION['id'];

			$result = $user -> add($sql);

			$data['id'] = $result;
			$data['errno'] = 1;

			echo json_encode($data);
		}

	}

	public function edit_edu() {
		//修改教育经历
		if (IS_POST) {

			foreach ($_POST as $value) {

				if ($value == '') {
					$data = 0;
					echo json_encode($data);
					return;
				}
			}
			$user = M('education');

			$sql = "UPDATE `education` SET `school`='$_POST[school_name]', `profession` = '$_POST[departments]',`year` = '$_POST[education_years]',`uid` ='$_SESSION[id]'WHERE `id` = '$_POST[eduid]'";
			$user -> execute($sql);

			$data['errno'] = 1;

			echo json_encode($data);
		}
	}

	public function edit_work() {
		//修改工作经历
		if (IS_POST) {

			foreach ($_POST as $value) {

				if ($value == '') {
					$data = 0;
					echo json_encode($data);
					return;
				}
			}
			$user = M('work');

			$sql = "UPDATE `work` SET `company`='$_POST[company_name]', `job` = '$_POST[job_id]',`start` = '$_POST[start_year]',`end` = '$_POST[end_year]' WHERE `id` = '$_POST[workEditId]'";

			$user -> execute($sql);

			$data['errno'] = 1;

			echo json_encode($data);
		}
	}

	public function remove_edu() {
		// 删除教育经历
		if (IS_POST) {

			$model = M('education');

			$model -> delete($_POST['id']);
		}
	}

	public function remove_work() {
		//删除工作经历
		if (IS_POST) {
			// var_dump($_POST);
			$model = M('work');

			$model -> delete($_POST['id']);
		}

	}

	public function upload() {
		//上传头像
		if (!empty($_FILES)) {

			$config = array('maxSize' => 3145728, 'rootPath' => './Uploads/', //设置上传文件路径
			'savePath' => '', 'saveName' => array('uniqid', ''), //生成唯一图片名字
			'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date', 'Ymd'), );
			$upload = new \Think\Upload($config);
			// 实例化上传类

			$info = $upload -> upload();

			if (!$info) {// 上传错误提示错误信息
				$this -> error($upload -> getError());
			} else {// 上传成功

				$info = current($info);

				$path = './Uploads/' . $info['savepath'] . $info['savename'];

				// move_uploaded_file($_FILES['upload_file']['tmp_name'], $path);

				$sql = "UPDATE `user` SET `head_img` = '{$path}' WHERE `id` = '{$_SESSION['id']}'";

				$model = M('user');

				$model -> execute($sql);

				$_SESSION['head_img'] = $path;

				echo ltrim($path, '.');

			}
		} else {

			$this -> display();
		}
	}

	public function security() {
		

		//修改密码，先确认原来密码没输错
		if (IS_POST) {

			$password = md5($_POST['re_password']);
			$sql = "UPDATE `user` SET `password` = '$password' WHERE `id` = '$_SESSION[id]'";
			$model = M('user');
			$result = $model -> execute($sql);
			if (!$result) {
				$this -> error('发生了未知的错误！即将返回当前页面！');
			} else {
				$this -> success('', U('/Home/Setting/security', 0));
			}
		} else {
			$this -> display();
		}
	}

	public function security_ajax() {

		// static $num = 1;

		// if($num <= 3){
		// 操作次数,暂时不处理
		// $num++;

		$model = M('user');

		$sql = "SELECT `password` from `user` WHERE `id` = '$_SESSION[id]'";

		$result = $model -> query($sql);

		if (md5(I('post.password')) == $result[0]['password']) {

			echo json_encode(1);
		} else {

			echo json_encode(2);
		}
		// }else{
		// echo json_encode(3);

		// }
	}

	public function verify() {
		//认证资料上传，有认证状态字段，只有认证通过才会向user表插入真实姓名
		$sql = "SELECT `state`,`truename` FROM `verify` WHERE uid = '{$_SESSION['id']}'";
		$model = M('verify');
		$result = $model -> query($sql);

		if ($result[0]['state'] == 2 && empty($_SESSION['truename'])) {
			$model = M('user');
			$sql = "UPDATE `user` SET `truename` = '{$result[0]['truename']}' WHERE `id` = '{$_SESSION['id']}'";
		} else if ($result[0]['state'] == 1) {
			$_SESSION['state'] = 1;
		}

		if (IS_POST) {

			if ($_FILES['size'] != 0 || $_FILES['error'] != 4) {
				// 附件不为空，就执行操作
				if ($_FILES['error'] != 0) {

					$this -> error('文件上传失败,请重新上传！', U('Home/Setting/verify'), 1);

				} else {

					$config = array('maxSize' => 3145728, 'rootPath' => './Run_files/', //设置上传文件路径
					'savePath' => '', 'saveName' => array('uniqid', ''), //生成唯一图片名字
					'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => true, 'subName' => array('date', 'Ymd'), );
					$upload = new \Think\Upload($config);
					// 实例化上传类

					$info = $upload -> upload();
					if (!$info) {

						$this -> error($upload -> getError(), U('Home/Setting/verify'), 1);
					} else {

						$info = current($info);

						$path = './Run_files/' . $info['savepath'] . $info['savename'];
						//认证资料添加
						$this -> _insert($_POST, $path);

						$this -> success('资料+附件上传成功', U('Home/Setting/verify'));
					}
				}
			} else {
				$this -> _insert($_POST);
				$this -> success('资料上传成功', U('Home/Setting/verify'));
			}
		} else {

			$this -> display();
		}
	}

	private function _insert($data, $path = '') {
		//认证资料添加
		foreach ($data as $value) {
			//认证资料每项都不能为空
			if ($value == '') {
				$this -> error('资料必须填写完整，请重新填写', U('Home/Setting/verify'), 1);
			}
		}

		if (!is_numeric($data['idcard']) || !is_numeric($data['phone'])) {

			$this -> error('身份证或者电话错误');
		}
		// $sql = "INSERT INTO `verify`( `type`, `truename`, `idcard`, `phone`, `instruction`, `uid`, `state`)VALUES('$_POST[type]', '$_POST[name]', '$_POST[idcard]', '$_POST[phone]', '$_POST[instruction]','$_SESSION[id]')", '1';

		// $model -> execute($sql);
		$model = M('verify');
		$sql['type'] = $data['type'];
		$sql['truename'] = $data['name'];
		$sql['idcard'] = $data['idcard'];
		$sql['phone'] = $data['phone'];
		$sql['instruction'] = $data['instruction'];
		$sql['uid'] = $_SESSION['id'];
		$sql['state'] = '1';
		$sql['files'] = $path;
		//不管文件是否为空，都先插入数据
		$result = $model -> add($sql);

		if (!$result) {

			$this -> error('未知错误导致资料上传失败！', U('Home/Setting/verify'), 1);
		}

	}

}
?>